{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Distance Images\n",
    "\n",
    "These illustrate a few of the distance computations used for k-NN algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: MacOSX\n"
     ]
    }
   ],
   "source": [
    "%matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from math import hypot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Euclidean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(\"Euclidean\", (5, 5))\n",
    "plt.set_cmap('gray')\n",
    "\n",
    "plt.axis((6, 10, 6, 10))\n",
    "plt.xticks((6, 7, 8, 9, 10))\n",
    "plt.yticks((6, 7, 8, 9, 10))\n",
    "plt.grid(axis='both')\n",
    "plt.scatter((7, 8), (8, 9), marker=\"o\", label=\"??\", color='0.0')\n",
    "\n",
    "plt.annotate('(7, 8)', xy=(7, 8), xytext=(7, 7.8))\n",
    "plt.annotate('(8, 9)', xy=(8, 9), xytext=(8, 8.8))\n",
    "\n",
    "plt.annotate(\"\", xy=(8, 9), xytext=(7, 8), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "plt.text(7, 8.5, \"Euclidean = 1.4\", bbox=dict(boxstyle='square', fc='white', ec='white', pad=0.2), color='black')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manhattan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(\"Manhattan\", (5, 5))\n",
    "plt.set_cmap('gray')\n",
    "\n",
    "plt.axis((6, 10, 6, 10))\n",
    "plt.xticks((6, 7, 8, 9, 10))\n",
    "plt.yticks((6, 7, 8, 9, 10))\n",
    "plt.grid(axis='both')\n",
    "plt.scatter((7, 8), (8, 9), marker=\"o\", label=\"??\", color='0.0')\n",
    "\n",
    "plt.annotate('(7, 8)', xy=(7, 8), xytext=(7, 7.8))\n",
    "plt.annotate('(8, 9)', xy=(8, 9), xytext=(8, 8.8))\n",
    "\n",
    "plt.annotate(\"\", xy=(7, 9), xytext=(7, 8), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "plt.annotate(\"\", xy=(8, 9), xytext=(7, 9), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "\n",
    "plt.text(7, 8.5, \"Manhattan = 2.0\", bbox=dict(boxstyle='square', fc='white', ec='white', pad=0.2), color='black')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chebyshev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(\"Chebyshev\", (5, 5))\n",
    "plt.set_cmap('gray')\n",
    "\n",
    "plt.axis((6, 10, 6, 10))\n",
    "plt.xticks((6, 7, 8, 9, 10))\n",
    "plt.yticks((6, 7, 8, 9, 10))\n",
    "plt.grid(axis='both')\n",
    "plt.scatter((7, 8), (8, 9), marker=\"o\", label=\"??\", color='0.0')\n",
    "\n",
    "plt.annotate('(7, 8)', xy=(7, 8), xytext=(7, 7.8))\n",
    "plt.annotate('(8, 9)', xy=(8, 9), xytext=(8, 8.8))\n",
    "\n",
    "plt.annotate(\"\", xy=(8, 9), xytext=(7, 8), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "plt.text(7, 8.5, \"Chebyshev = 1.0\", bbox=dict(boxstyle='square', fc='white', ec='white', pad=0.2), color='black')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sorensen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(\"Sorensen\", (5, 5))\n",
    "plt.set_cmap('gray')\n",
    "\n",
    "plt.axis((0, 10, 0, 10))\n",
    "plt.xticks(list(range(11)))\n",
    "plt.yticks(list(range(11)))\n",
    "plt.grid(axis='both')\n",
    "plt.scatter((6, 8), (7, 9), marker=\"o\", label=\"??\", color='0.0')\n",
    "\n",
    "plt.annotate('(6, 7)', xy=(6, 7), xytext=(6, 6.8), va='top')\n",
    "plt.annotate('(8, 9)', xy=(8, 9), xytext=(8, 8.8), va='top')\n",
    "\n",
    "plt.annotate(\"\", xy=(6, 9), xytext=(6, 7), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "plt.annotate(\"\", xy=(8, 9), xytext=(6, 9), arrowprops=dict(width=2.5, shrink=.1, color='black'))\n",
    "\n",
    "plt.text(4.5, 5.6, \"Manhattan = 4\\nSorensen = (2+2)/(14+16)\", bbox=dict(boxstyle='square', fc='white', ec='white', pad=0.2), color='black')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
